package com.yjb.app.wxapi;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.xiaomi.mipush.sdk.Constants;
import com.zxl.yjb.R;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.MessageEvent;
import com.zxl.yjb.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;


public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler, View.OnClickListener {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	private TextView tv_result;
	private Button btnBackHome;
	private Button btnBackList;
	private Button btnShare;
	private String payResult;



	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
		tv_result = findViewById(R.id.tv_result);
		btnBackList = findViewById(R.id.btn_back_list);
		btnBackHome = findViewById(R.id.btn_back_home);
		btnShare = findViewById(R.id.btn_share);
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
		payResult = getIntent().getStringExtra("payResult");//支付宝结果
		initView();
    }

	@Override
	protected void initView() {
//		btnBackList.setOnClickListener(this);
		btnBackHome.setOnClickListener(this);
//		btnShare.setOnClickListener(this);
        btnBackList.setVisibility(View.GONE);
        btnShare.setVisibility(View.GONE);
		if (TextUtils.isEmpty(payResult))
			return;//如果为空说明不是支付宝回调，return
		if ("fail".equals(payResult)){
			tv_result.setText("支付失败，如果已经扣款，请及时联系客服人员");
			ToastUtils.show(WXPayEntryActivity.this, "支付失败，如果已经扣款，请及时联系客服人员");
			sendFailEvent();
		}else {
			ToastUtils.show(WXPayEntryActivity.this, "支付成功");
			sendSuccessEvent();
		}

	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if (resp.errCode == 0) {
				ToastUtils.show(WXPayEntryActivity.this, "支付成功");
				sendSuccessEvent();
			} else if (resp.errCode == -1) {
				tv_result.setText("支付失败，如果已经扣款，请及时联系客服人员");
				ToastUtils.show(WXPayEntryActivity.this, "支付失败，如果已经扣款，请及时联系客服人员");
				sendFailEvent();
			} else if (resp.errCode == -2) {
				ToastUtils.show(WXPayEntryActivity.this, "取消支付");
				sendCancleEvent();
				finish();
			}
		}
	}

	private void sendSuccessEvent() {
		EventBus.getDefault().post(new MessageEvent("payfinish"));
		EventBus.getDefault().post(new MessageEvent("paySuccess"));
	}

	private void sendFailEvent() {
		EventBus.getDefault().post(new MessageEvent("payfinish"));
		EventBus.getDefault().post(new MessageEvent("payFail"));
	}

	private void sendCancleEvent() {
		EventBus.getDefault().post(new MessageEvent("payfinish"));
		EventBus.getDefault().post(new MessageEvent("payCancle"));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_back_list:
				break;
			case R.id.btn_back_home:
				sendCancleEvent();
				finish();
				break;
			case R.id.btn_share:
				break;
		}
	}
}